<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="top_header_left">
        <el-select v-model="medal_index.cat_id" placeholder="商品类型" clearable class="filter-item" filterable
          style="width: 200px;max-width:70%;margin-right: 20px">
          <el-option v-for="item in classType" :key="item.cat_id" :label="item.cat_name" :value="item.cat_id" />
        </el-select>
        <el-input v-model="medal_index.keyword" placeholder="礼物名称" style="width:200px;margin-right: 20px"
          @keyup.enter.native="handleFilter" />
        <el-button class="filter-item" type="primary" icon="el-icon-search"
          @click="medal_index.page = 1;handleFilter()">
          搜索
        </el-button>
      </div>
      <div class="top_header_right">
        <el-button class="filter-item" type="primary" style="margin-left: 20px" @click="setUp(1)">
          新增
        </el-button>
      </div>
    </div>

    <el-table ref="multipleTable" :data="list" stripe style="width: 1400px" border>
      <el-table-column prop="goods_id" label="id" width="80" align="center" :show-overflow-tooltip='true' />
      <el-table-column prop="goods_name" label="名称" width="200" align="center">
        <template align="center" slot-scope="scope">
          <div class="demo-image__preview">
            <el-image class="my-img" :src="scope.row.image_full" ref="myImg" :preview-src-list="srcList"
              @click="handlePriveImg(scope.row.image_full)" @click.stop="handleClickItem">
            </el-image>
            <span>{{scope.row.goods_name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="cat_id" label="类型" align="center">
        <template align="center" slot-scope="scope">
          <span v-if="scope.row.cat_id == 0">未知</span>
          <span v-if="scope.row.cat_id == 1">座驾</span>
          <span v-if="scope.row.cat_id == 2">头像框</span>
          <span v-if="scope.row.cat_id == 3">积分兑换</span>
          <span v-if="scope.row.cat_id == 4">进场特效</span>
        </template>
      </el-table-column>
      <el-table-column prop="diamond" label="钻石" align="center">
        <template align="center" slot-scope="scope">
          <span>{{scope.row.diamond}}钻</span>
        </template>
      </el-table-column>
      <el-table-column prop="day" label="有效期" align="center">
        <template align="center" slot-scope="scope">
          <span>{{scope.row.day}}天</span>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center" width="100" />

      <el-table-column prop="is_show" label="是否显示" align="center" width="100">
        <template align="center" slot-scope="scope">
          <span v-if="scope.row.is_show == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" align="center" width="300">
        <template align="center" slot-scope="scope">
          <el-button type="button" size="small" @click="setUp(2,scope.row)">
            编辑
          </el-button>
          <el-button type="button" size="small" @click="medalSend_(scope.row)">
            发放
          </el-button>
          <el-button type="button" size="small" @click="delete_(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="medal_index.page" :limit.sync="medal_index.pageSize"
      @pagination="handleFilter" />

    <el-dialog title="发放" :visible.sync="dialogFormVisible1" style="width:800px;margin:0 auto;" class="model_">
      <div class="center_lisr">
        <div>
          <div class="box_left">用户id</div>
          <el-input v-model="add_match.perfectNumber" placeholder="用户id" style="width:300px;max-width:70%;"
            type="number" />
        </div>
        <div>
          <div class="box_left">时间</div>
          <el-input v-model="add_match.days" placeholder="时间" style="width:300px;max-width:70%;" type="number" />
        </div>

      </div>
      <div>
        <el-button class="filter-item" type="primary" style="width:100%;margin:0 auto;margin-top:10px" @click="baocun">
          发放
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  import {
    encryption2
  } from "@/api/index";
  export default {
    name: "shopping_",
    components: {
      Pagination,
    },
    data() {
      return {
        classType: [{
          cat_id: 0,
          cat_name: '全部'
        }, {
          cat_id: 1,
          cat_name: '座驾'
        }, {
          cat_id: 2,
          cat_name: '头像框'
        }, {
          cat_id: 3,
          cat_name: '个性名片'
        }, {
          cat_id: 4,
          cat_name: '进场特效'
        }, ],
        total: 0,
        calendarTypeOptions1: [],
        list: [],
        time_list: {},

        add_match: {
          perfectNumber: '',
          days: '',
        },
        medal_index: {
          cat_id: 0,
          keyword: '',
          page: 1,
          pageSize: 10,
        },
        dialogFormVisible1: false,
        srcList: [],
        fileList: [],
        item_username: {}, //发放内容
      };
    },

    created() {
      this.handleFilter();
    },
    methods: {
      delete_(item) {
        let that = this;
        this.$confirm(`此操作将永久删除 ${item.goods_name}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.delet_over(item.goods_id)
        }).catch(() => {

        });

      },
      delet_over(goodsId) {
        const arys = {
          goodsId: goodsId,
        };
        const url = "/good/goodDelete";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.$message({
              message: res.data.text,
              type: "success",
            });
            this.handleFilter()
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      baocun() {
        const arys = {
          perfectNumber: this.add_match.perfectNumber,
          days: this.add_match.days,
          goodsId: this.item_username.goods_id,
        };
        const url = "/good/goodSend";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.dialogFormVisible1 = false;
            this.add_match.perfectNumber = '',
              this.add_match.days = '',
              this.$message({
                message: res.data.text,
                type: "success",
              });
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
      medalSend_(item) {
        // console.log(item);
        this.dialogFormVisible1 = true;
        this.item_username = item;
      },
      handlePriveImg(row) {
        this.srcList = []
        this.srcList.push(row); //将后端的这一行的数据push进数组中
        this.$refs.myImg.clickHandler();
      },
      // 获取图片遮罩层dom
      handleClickItem() {
        setTimeout(function () {
          let domImageMask = document.querySelector(".el-image-viewer__wrapper");
          if (!domImageMask) {
            return;
          }
          domImageMask.addEventListener("click", (e) => {
            if (e.target.parentNode.className == 'el-image-viewer__actions__inner' || e.target.parentNode
              .className == 'el-image-viewer__btn el-image-viewer__next' || e.target.parentNode.className ==
              'el-image-viewer__btn el-image-viewer__prev') {
              return; //如果点击底部菜单，不关闭
            }
            // 点击遮罩层时调用关闭按钮的 click 事件
            document.querySelector(".el-image-viewer__close").click();
            document.querySelector('body').setAttribute('style', 'overflow:auto;')
          });
        }, 300)
      },

      setUp(type, row) {
        this.$router.push({
          path: '/shopping/shopping_list',
          name: 'shopping_list',
          query: {
            type: type,
            row: row,
          }
        })
      },
      // 查询
      handleFilter() {
        const arys = {
          cat_id: this.medal_index.cat_id,
          keyword: this.medal_index.keyword,
          page: this.medal_index.page,
          pageSize: this.medal_index.pageSize,
        };
        const url = "/good/goodList";
        encryption2(arys, url).then((res) => {
          if (res.data.status === 0) {
            this.list = res.data.result.list;
            this.total = res.data.result.total;
          } else {
            this.$message({
              message: res.data.text,
              type: "error",
            });
          }
        });
      },
    },
  };
</script>
<style scoped>
  .filter-container .filter-item {
    margin-bottom: 0;
  }

  .el-table--border {
    border-right: 1px solid #dfe6ec;
  }

  .el-alert {
    width: 1400px;
  }

  .filter-container {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 1400px;
  }

  .batch {
    width: 1200px;
    margin: 10px 0;
    display: flex;
    flex-direction: row-reverse;
  }

  .batch .filter-item {
    margin-left: 20px;
  }



  .flex_de>div {
    display: flex;
    flex-direction: column;
  }

  .center_lisr>div {
    display: flex;
    width: 100%;
    align-items: center;
    /* justify-content: space-between; */
    margin: 18px 0;
  }

  .center_lisr>div>el-input {
    flex: 1;
    margin-left: 20px;
  }

  .box_left {
    width: 80px;
    height: 40px;
    display: flex;
    align-items: center;
  }

  .model_ /deep/ .el-dialog__body {
    padding-top: 0%;
  }

  .model_ /deep/ .el-button--mini {
    padding: 10px 20px;
  }

  .model_ /deep/ .el-dialog {
    border-radius: 4px;
  }

  .model_ /deep/ .el-input__inner {
    /* text-align: center; */
  }

  .flex_de /deep/ .el-input__inner {
    padding: 0 4px;
    border: 0;
    background: 00000000;
  }

  .new_row {
    width: 80%;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: #333;
    font-weight: blod;
    font-family: serif;
    cursor: pointer;
  }

  .my-img {
    width: 80px;
    /* height: 80px; */
    padding: 10px;
  }

  .image_new_100 {
    height: 100px;
    display: flex;
    align-items: center;
  }

  .image_new_100>img {
    width: 50px;
    margin-right: 10px;
  }

  .demo-image__preview {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .long_icon .my-img {
    width: 150px;
  }
</style>